//引入express模块
const express = require("express");
//创建购物车路由器对象
const cart = express.Router();
//引入数据库连接池
const cartPool = require("../pool.js");

//1.添加购物车添加路由(post /reg)
cart.post('/reg',(req,res,next)=>{
	//1.1接受流传递的数据
	console.log(req.body);
	let obj = req.body;
	//1.2判断添加数据是否为空
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//1.3执行SQL命令
	cartPool.query('INSERT INTO aa_shopping_cart SET ?',[obj],(err,result)=>{
		if(err){
			//错误出现，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否添加成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"添加失败"});
		}else{
			res.send({"code":200,"msg":"添加成功"});
		}
	});
});

//2。添加购物车修改路由(put /)
cart.put('/',(req,res,next)=>{
	//2.1接受流传递的数据
	console.log(req.body);
	let obj = req.body;
	let $cid = req.body.cid;
	//2.2判断修改数据是否为空
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//2.3执行SQL命令
	cartPool.query('UPDATE aa_shopping_cart SET ? WHERE cid=?',[req.body,$cid],(err,result)=>{
		if(err){
			//错误出现，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否修改成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"修改失败"});
		}else{
			res.send({"code":200,"msg":"修改成功"});
		}		
	});
});

//3.添加购物车删除路由(delete /:cid)
cart.delete('/:cid',(req,res,next)=>{
	//3.1接受路由传参传递的数据
	console.log(req.params);
	var cid = req.params.cid;
	//3.2执行SQL命令
	cartPool.query('DELETE FROM aa_shopping_cart WHERE cid=?',[cid],(err,result)=>{
		if(err){
			//错误出现，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否删除成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}		
	});		
});

//4添加购物车查询路由(get /)
cart.get('/',(req,res,next)=>{
	//4.1获取查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//4.2执行SQL命令
	cartPool.query('SELECT * FROM aa_shopping_cart WHERE ?',[obj],(err,result)=>{
		if(err){
			//出现错误，将错误保存传递给错误处理中间件
			next(err);
			return;
		}
		//通过affectedRows是否等于零判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询失败","data":result});
		}
	});
});

//5.添加购物车列表查询路由(get /list)
cart.get('/list',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//5.2判断是否为空
	if(!obj.pno) obj.pno = 1;
	if(!obj.count) obj.count = 5;
	//5.3计算开始查询的值
	let start = (obj.pno-1)*obj.count;
	//5.4将每页数据量装为整形
	let size = parseInt(obj.count);
	//5.5执行SQL命令
	cartPool.query('SELECT * FROM aa_shopping_cart LIMIT ?,?',[start,size],(err,result)=>{
		if(err){
			//出错时将错误传递给错误处理中间件
			next(err);
			return;
		}
		//判断是否删除成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询失败","data":result});
		}		
	});
});

//6.添加获取所有购物车信息(get /cartlist)
cart.get('/cartlist',(req,res,next)=>{
	//6.1获取查询字符串传递的数据
	console.log(req.query);
	//6.2执行SQL命令
	cartPool.query('SELECT * FROM aa_shopping_cart',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询失败","data":result});
		}
	});
});

//导出用户地址路由器对象
module.exports = cart;